<?php

    class Admin_CustomersController extends Zend_Controller_Action {

        public function init() {
            /* Initialize action controller here */
        }

        public function indexAction() {
            // action body
            $customer = new Admin_Model_CustomerMapper();
            if (isset($_GET['txtSearch'])) {
                //echo $_GET['txtSearch'];
                $keyword = $_GET['txtSearch'];
                echo $keyword;
                $arr = $customer->_search($keyword);
                $this->view->assign('customers', $arr);
            } else {
                $arr = $customer->_getAll();
                $this->view->assign('customers', $arr);
            }

        }

        public function addAction() {
            $this->_helper->layout()->disableLayout();
            $customer = new Admin_Model_CustomerMapper();
            if (isset($_POST['submit'])) {
                $this->_helper->viewRenderer->setNoRender(true);
                $username = $_POST['txtUsername'];
                $password = $_POST['txtPassword'];
                $fname = $_POST['txtFirstname'];
                $lname = $_POST['txtLastname'];
                $email = $_POST['txtEmail'];
                $phone = $_POST['txtPhone'];
                $fax = $_POST['txtFax'];
                $add = $_POST['txtAdd'];
                $add2 = $_POST['txtAdd2'];
                $city = $_POST['txtCity'];
                $birthday = $_POST['txtBirthday'];
                if (isset($_POST['chkUser'])) {
                    $isUser = 1;
                } else {
                    $isUser = 0;
                }
                $customer->_insert($username, $password, $fname, $lname, $email, $phone, $fax, $add, $add2, $city, $isUser, $birthday);
                echo 'OK';
                exit();
            }
        }

        public function deleteAction() {
            // action body
            $arr = $_GET['chkCustomer'];
            $customer = new Admin_Model_CustomerMapper();
            foreach ($arr as $id) {
                $customer->_delete($id);
            }
            header("location: /aitishop/admin/customers");
        }

        public function updateAction() {
            // action body
            $this->_helper->layout()->disableLayout();
            $customer = new Admin_Model_CustomerMapper();
            if (isset($_POST['submit'])) {
                $this->_helper->viewRenderer->setNoRender(true);
                $id = $_POST['txtId'];
                $username = $_POST['txtUsername'];
                $password = $_POST['txtPassword'];
                $fname = $_POST['txtFirstname'];
                $lname = $_POST['txtLastname'];
                $email = $_POST['txtEmail'];
                $phone = $_POST['txtPhone'];
                $fax = $_POST['txtFax'];
                $add = $_POST['txtAdd'];
                $add2 = $_POST['txtAdd2'];
                $city = $_POST['txtCity'];
                $birthday = $_POST['txtBirthday'];
                if (isset($_POST['chkUser'])) {
                    $isUser = 1;
                } else {
                    $isUser = 0;
                }
                $customer->_update($id, $username, $password, $fname, $lname, $email, $phone, $fax, $add, $add2, $city, $isUser, $birthday);
                echo 'OK';
            } else {
                $id = $_GET['id'];
                $result = $customer->_getById($id);
                $this->view->assign('customer', $result);
            }
        }

    }
